/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2025. All rights reserved.
 *
 */
import { HvigorTaskGroupType } from '@ohos/hvigor/src/base/external/task/core-task.js';

import { ECM } from './error-code-map.js';

export class ErrorCode {
  private apiType: ECM.ApiTypeKey = 'unexpected';
  private moduleType: ECM.ModuleTypeKey = 'unexpected';
  private taskGroup: string = HvigorTaskGroupType.OTHER_TASK_GROUP;
  private detailsError: ECM.DECE = ECM.DECE.UNEXPECTED;
  private static instance: ErrorCode;

  public static getInstance(): ErrorCode {
    if (!ErrorCode.instance) {
      ErrorCode.instance = new ErrorCode();
    }
    return ErrorCode.instance;
  }

  /**
   * 生成错误码
   * @returns {string}
   */
  createErrorCode(): string {
    const apiTypeCode: string = ECM.getApiTypeEC(this.getApiType());
    const moduleTypeCode: string = ECM.getModuleTypeEC(this.getModuleType());

    // 未解决
    const taskGroupCode = ECM.getTaskGroupEC(this.getTaskGroup());
    const detailsErrorCode = this.getDetailsError();
    return apiTypeCode + moduleTypeCode + taskGroupCode + detailsErrorCode;
  }

  public reset(): void {
    this.setApiType('unexpected');
    this.setModuleType('unexpected');
    this.setTaskGroup(HvigorTaskGroupType.OTHER_TASK_GROUP);
    this.setDetailsError(ECM.DECE.UNEXPECTED);
  }

  public getApiType(): ECM.ApiTypeKey {
    return this.apiType;
  }

  public setApiType(apiType: ECM.ApiTypeKey): void {
    this.apiType = apiType;
  }

  public getModuleType(): ECM.ModuleTypeKey {
    return this.moduleType;
  }

  public setModuleType(module: ECM.ModuleTypeKey): void {
    this.moduleType = module;
  }

  public getDetailsError(): ECM.DECE {
    return this.detailsError;
  }

  public setDetailsError(detailsError: ECM.DECE): void {
    this.detailsError = detailsError;
  }

  public getTaskGroup(): string {
    return this.taskGroup;
  }

  public setTaskGroup(taskGroup: string): void {
    this.taskGroup = taskGroup;
  }
}
